﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Xml.Linq;

namespace GameTank
{
    public class ConstructionDTO
    {
        string filePathMD = "brick.mdd";

        public string FilePathMD
        {
            get { return filePathMD; }
            set { filePathMD = value; }
        }

        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private bool _breakable;

        public bool Breakable
        {
            get { return _breakable; }
            set { _breakable = value; }
        }

        private Vector2 _pos;

        public Vector2 Pos
        {
            get { return _pos; }
            set { _pos = value; }
        }
        private float _hp;

        public float Hp
        {
            get { return _hp; }
            set { _hp = value; }
        }

        float _currentHp;

        public float CurrentHp
        {
            get { return _currentHp; }
            set { _currentHp = value; }
        }

        private float _defense;

        public float Defense
        {
            get { return _defense; }
            set { _defense = value; }
        }

        public ConstructionDTO(ConstructionDTO orther)
        {

            if (orther != null)
            {
                this._breakable = orther._breakable;
                this._defense = orther._defense;
                this._hp = orther._hp;
                this._currentHp = orther._currentHp;
                this._pos = orther._pos;
                this._name = orther._name;
                this._layer = orther._layer;
                this._broken = orther._broken;
            }
            else
                khoiTaoRong();
        }

        public ConstructionDTO()
        {
            khoiTaoRong();
        }

        private void khoiTaoRong()
        {
            this._name = "brick";
            this._pos = new Vector2(0, 0);
            this._breakable = true;
            this._defense = 0;
            this._hp = 100;
            this._currentHp = 100;
        }

        public ConstructionDTO(string name, Vector2 pos, bool breakable, float defense, float hp, float currentHp, int layer)
        {
            this.Name = name;
            this.Pos = pos;
            this.Breakable = breakable;
            this.Defense = defense;
            this.Hp = hp;
            this.CurrentHp = currentHp;
            this.Layer = layer;
        }

        bool _broken;

        public bool Broken
        {
            get
            {
                if (CurrentHp <= 0)
                    _broken = true;
                else
                    _broken = false;
                return _broken;
            }

            set { }
        }

        int _layer;

        public int Layer
        {
            get { return _layer; }
            set { _layer = value; }
        }

        public XElement ToXml()
        {
            XElement element = new XElement("Construction");

            element.SetAttributeValue("filePath", this.FilePathMD);

            element.SetAttributeValue("name", this.Name);

            element.SetAttributeValue("breakable", this.Breakable);

            element.Add(new XElement("pos", new XAttribute("x", this.Pos.X), new XAttribute("y", this.Pos.Y)));

            element.SetAttributeValue("hp", this.Hp);

            element.SetAttributeValue("currenthp", this.CurrentHp);

            element.SetAttributeValue("defense", this.Defense);

            element.SetAttributeValue("broken", this.Broken);

            element.SetAttributeValue("layer", this.Layer);

            return element;
        }

        // luw doituong xuong thanh file
        public void Save(string fileName)
        {
            XDocument doc = new XDocument();            
            doc.Add(ToXml());
            doc.Save(fileName);
        }

        public static ConstructionDTO Parse(XElement element)
        {
            ConstructionDTO construction = new ConstructionDTO();

            construction.filePathMD = element.Attribute("filePath").Value;

            construction.Name = element.Attribute("name").Value;

            construction.Breakable = bool.Parse(element.Attribute("breakable").Value);

            int x = int.Parse(element.Element("pos").Attribute("x").Value);
            int y = int.Parse(element.Element("pos").Attribute("y").Value);
            construction.Pos = new Vector2(x, y);

            construction.Hp = float.Parse(element.Attribute("hp").Value);

            construction.CurrentHp = float.Parse(element.Attribute("currenthp").Value);

            construction.Defense = float.Parse(element.Attribute("defense").Value);

            construction.Broken = bool.Parse(element.Attribute("broken").Value);

            construction.Layer = int.Parse(element.Attribute("layer").Value);

            return construction;
        }

        public void SaveToFile(String fileName)
        {
            XDocument doc = new XDocument();
            doc.Add(ToXml());
            doc.Save(fileName);
        }
    }
}
